package com.pignest.hotel.ui.income;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.hotel.beans.Living;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.definition.RoomUseStatus;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class InComeManageVbox extends Vbox implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2869645144357450481L;

	private ReceptionService receptionService;
	private Listbox LivingListbox;
	private Paging pagLiving;
	/**查询项****/
	private Textbox qRoomName;//房间名
	private Listbox qRoomTypeList;//房间类型
	private Listbox qLiveStateList;//入住状态
	private Listbox qOpenStateList;
	private Textbox qCustormName;//客户姓名
	private Listbox qGender;//性别
	private Datebox qStartLiveDate;//入住开始日期
	private Datebox qEndLiveDate;//入住截止日期
	/**按钮*/
	private Button comeOnLivingBtn;//续住
	private Button inComeManageBtn;//账务处理
	private Button editAccountBtn;//调账
	private Button inComeBtn;//入账
	private Button printAcountBtn;//打印账目
	private Button inCheckOutBtn;//结账/退房
	private Button hangUpBtn;//挂账 退房
	@Override
	public void onEvent(Event arg0) throws Exception {
		// TODO Auto-generated method stub

	}
	public InComeManageVbox()
	{
		receptionService =Services.getReceptionService();
	}
	  public void onCreate() {
	    	LivingListbox=(Listbox)getFellow("LivingListbox");
			pagLiving    =(Paging)getFellow("pagLiving");
			qRoomName    =(Textbox)getFellow("qRoomName");
			qRoomTypeList=(Listbox)getFellow("qRoomTypeList");
			qLiveStateList=(Listbox)getFellow("qLiveStateList");
			qOpenStateList=(Listbox)getFellow("qOpenStateList");
			qCustormName  =(Textbox)getFellow("qCustormName");
			qGender       =(Listbox)getFellow("qGender");
			qStartLiveDate=(Datebox)getFellow("qStartLiveDate");
			qEndLiveDate  =(Datebox)getFellow("qEndLiveDate");
			
			comeOnLivingBtn=(Button)getFellow("comeOnLivingBtn");
			inComeManageBtn=(Button)getFellow("inComeManageBtn");
			editAccountBtn=(Button)getFellow("editAccountBtn");
			inComeBtn=(Button)getFellow("inComeBtn");
			printAcountBtn=(Button)getFellow("printAcountBtn");
			inCheckOutBtn=(Button)getFellow("inCheckOutBtn");
			hangUpBtn=(Button)getFellow("hangUpBtn");
			final int PAGE_SIZE = pagLiving.getPageSize();
			pagLiving.addEventListener("onPaging", new EventListener() {
			      public void onEvent(Event event) {
			        	PagingEvent pe = (PagingEvent) event;
			        	int pgno = pe.getActivePage();
			        	int start = pgno*PAGE_SIZE;
			        	query(start,PAGE_SIZE);
			      	}
			    	});
				query(0,PAGE_SIZE);
	  }
	  public void query(int start,int pageSize)
		{
			LivingListbox.getItems().clear();
			String sRoomName =qRoomName.getValue();
			String sLivingState =(String)UIUtils.getSelectedValue(qLiveStateList);
			String sRoomTypeId =(String)UIUtils.getSelectedValue(qRoomTypeList);
			String sCustormName =(String)qCustormName.getValue();
	        String sSex =(String)UIUtils.getSelectedValue(qGender);
	        String oType =(String)UIUtils.getSelectedValue(qOpenStateList);
	        Date startLivingDate=qStartLiveDate.getValue();
	        Date endLivingDate=qEndLiveDate.getValue();
	        int Total =receptionService.queryLiving(sCustormName, sLivingState, sRoomName, sRoomTypeId, sSex, startLivingDate, endLivingDate,oType).size();
	        pagLiving.setTotalSize(Total);
	        List <Living> ls =receptionService.queryPageHQL(sCustormName, sLivingState, sRoomName, sRoomTypeId, sSex, startLivingDate, endLivingDate,oType, start, pageSize);
	        int index=1;
			for(final Living l:ls){
				  Listitem listitem = new Listitem();
				  listitem.setValue(l);
				  listitem.setParent(LivingListbox);
				  listitem.appendChild(new Listcell(String.valueOf(index)));
				  listitem.appendChild(new Listcell(l.getRoom().getName()));
				  listitem.appendChild(new Listcell(l.getRoom().getType().getName()));
				  listitem.appendChild(new Listcell(l.getCustormName()));
				  listitem.appendChild(new Listcell(OpenRoomType.valueOf(l.getOpenRoomType()).getDisplayName()));
				  listitem.appendChild(new Listcell(DateUtil.toFullString(l.getLiveTime())));
				  listitem.appendChild(new Listcell(DateUtil.toFullString(l.getLeaveDate())));
				  listitem.appendChild(new Listcell(DateUtil.toFullString(l.getExpectLeaveDate())));
				  listitem.appendChild(new Listcell(DateUtil.toFullString(l.getGrantDate())));
				  listitem.appendChild(new Listcell(MoneyUtil.toString(l.getAdjustPrice())));
				   
				  listitem.appendChild(new Listcell(RoomLiveStatus.valueof(l.getRoom().getLiveStatus()).getName()+RoomUseStatus.valueof(l.getRoom().getUseStatus()).getName()));
				  listitem.appendChild(new Listcell(LivingState.valueOf(l.getLiveState()).getDisplayName()));
				  listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
					   public void onEvent(Event event) throws Exception {
						   checkButton(l);
					   }
				   });
				  index++;
			}
		}
	  public void checkButton(Living l)
	  {
		  if(!l.getLiveState().equals(LivingState.CHECKOUT.name()))
		  {
			  //非结账可以：续住、账务处理、调账、入账、结账
			  comeOnLivingBtn.setDisabled(false);//续住
			  inComeManageBtn.setDisabled(false);//账务处理
			  editAccountBtn.setDisabled(false);//调账
			  inComeBtn.setDisabled(false);//入账
			  printAcountBtn.setDisabled(true);//打印账目
			  inCheckOutBtn.setDisabled(false);//结账/退房
			  if(l.getLiveState().equals(LivingState.HANDUP.name())){
				  hangUpBtn.setDisabled(true);//挂账退房
			  }else{
				  hangUpBtn.setDisabled(false);//挂账退房
			  }
		  }
		  else
		  {
			  comeOnLivingBtn.setDisabled(true);//续住
			  inComeManageBtn.setDisabled(true);//账务处理
			  editAccountBtn.setDisabled(true);//调账
			  inComeBtn.setDisabled(true);//入账
			  printAcountBtn.setDisabled(false);//打印账目
			  inCheckOutBtn.setDisabled(true);//结账/退房
			  hangUpBtn.setDisabled(true);
		  }
	  }
	  
}
